package PUBSUB_TWO.pubsubJms;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQTopic;

import javax.jms.*;

public class Consumer {

    public static String BROKER_URL = "tcp://0.0.0.0:61616";
    public static String USER = "admin";
    public static String PASSWORD = "admin";
    public static String DESITNATION = "SystemA.SystemB.Topics";
    public static void main(String[] args) throws JMSException, InterruptedException {
        //创建连接工厂
        ConnectionFactory factory = new ActiveMQConnectionFactory(BROKER_URL);
        //创建连接
        Connection connection = factory.createConnection(USER,PASSWORD);
        connection.start();
        //创建会话
        Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
        //消息目的地
        Destination destination = new ActiveMQTopic(DESITNATION);
        //创建消费者
        MessageConsumer messageConsumer = session.createConsumer(destination);

        messageConsumer.setMessageListener(new MessageListener() {
            public void onMessage(Message message) {
                    if (message instanceof TextMessage) {
                        String body;
                        try {
                            body = ((TextMessage) message).getText();
                            System.out.println(body);
                        } catch (JMSException e) {
                            e.printStackTrace();
                        }
                    } else {
                        System.out.println("Unexpected message type: " + message.getClass());
                    }
            }
        });

        while (true) {
            Thread.sleep(1 * 60 * 1000);
            break;
        }
        //释放资源
        session.close();
        connection.close();
        System.exit(0);
    }
}
